﻿// <copyright file="EnumTag.cs" company="Signal Processing Lab, Wuhan University">
// Copyright (c) 2009-2010 All Right Reserved
// </copyright>
// <author>Xinping Deng</author>
// <email>dentoby@gmail.com</email>
// <date>2010-4</date>
// <summary>This is part of Image.NET project.</summary>

namespace SPLWHU.Core
{
    using System;
    using System.Collections.Generic;

    /// <summary>
    /// The <see cref="Tag"/> with a value of <see cref="Enum"/> type.
    /// </summary>
    /// <typeparam name="T">Type of the value</typeparam>
    public class EnumTag<T> : Tag<T>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="EnumTag&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        public EnumTag(string name)
            : base(name)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="EnumTag&lt;T&gt;"/> class.
        /// </summary>
        /// <param name="name">The name of this tag.</param>
        /// <param name="value">The value to be set.</param>
        public EnumTag(string name, T value)
            : base(name, value)
        {
        }

        /// <summary>
        /// Gets all the strings of the enumeration.
        /// </summary>
        /// <returns>The array of string literals</returns>
        public string[] GetStrings()
        {
            return Enum.GetNames(this.ValueType);
        }

        /// <summary>
        /// Converts the value of this instance to text.
        /// </summary>
        /// <returns>Converted text representing the value.</returns>
        public override string ValueToText()
        {
            return this.Value.ToString();
        }

        /// <summary>
        /// Parses the value of this instance from text.
        /// </summary>
        /// <param name="text">The text to be parsed to generate value.</param>
        public override void ValueFromText(string text)
        {
            try
            {
                this.Value = (T)Enum.Parse(this.ValueType, text);
            }
            catch
            {
                throw new ParseException("Can not parse a " + this.ValueType.ToString() + "from " + text);
            }
        }
    }
}
